var express = require('express');
var router = express.Router();
const storeService = require('../service/storeService');

/* 获取门店数据 */
router.get('/getstore', async function (req, res) {
  let { current, pageSize, type, value } = req.query;
  let searchParams = {};
  if (type) {
    if (type == "phone") {
      searchParams[type] = value;
    } else {
      searchParams[type] = { $regex: value };
    }
  }
  let data = await storeService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
  res.send(data);

});

// id查找
router.get('/findbyid', async function (req, res) {
  let { id } = req.query;
  let data = await storeService.findById(id);
  res.send(data);
});

// 添加
router.post('/insert', async function (req, res) {
  let { name, address, phone, longitude, latitude, mapState, isOpen } = req.body;
  console.log(name, address, phone, longitude, latitude, mapState, isOpen);
  let data = await storeService.insert({ name, address, phone, longitude, latitude, mapState, isOpen });
  res.send(data);
});


// 删除
router.delete('/remove', async function (req, res) {
  let { id } = req.body;
  console.log(id);
  let data = await storeService.delete(id);
  res.send(data);
});

// 修改
router.put('/update', async function (req, res) {
  let { _id, name, address, phone, longitude, latitude, mapState, isOpen } = req.body;
  console.log(_id, name, address, phone, longitude, latitude, mapState, isOpen)
  let data = await storeService.update({ _id, name, address, phone, longitude, latitude, mapState, isOpen });
  res.send(data);
});



module.exports = router;
